﻿#include "widget.h"
#include "ui_widget.h"

#include <QPainter>
#include <QTimer>
#include <QtMath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(800,600);
    currentValue = 0;
    flag = 1;
    radius = height()/2;
    angle = 240*1.0 / 60;//1.0的作用是扩大变量类型,把int型变为double，保留小数
    startAngle = 150;

    startSpeed();

}

Widget::~Widget()
{
    delete ui;
}

//1.初始化画布
void Widget::initCanvas(QPainter &painter){

    painter.setRenderHint(QPainter::Antialiasing,true);//抗锯齿
    //底色变成黑色
    painter.setBrush(Qt::black);//设置黑色画刷
    painter.drawRect(rect());//画一个框

    //坐标系 平移到中心
    QPoint cent(rect().width()/2,rect().height()*0.6);
    painter.translate(cent);
}

//画中间的圆
void Widget::drawMiddleCirle(QPainter &painter, int radius)
{
    //画大圈
    //painter.drawEllipse(QPoint(0,0),radius,radius);
    //painter.setBrush(Qt::NoBrush);//将画刷丢掉，不然影响下个圆

    //画小圈
    painter.setPen(QPen(Qt::white,3));//设置白色笔，3粗
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

//8.画中间当前的数值
void Widget::drawCurrenSpeed(QPainter &painter)
{
    //当前值
    painter.setPen(Qt::white);
    QFont font = QFont("Arial",30);
    font.setBold(true);
    painter.setFont(font);
    //painter.drawText(0,0,QString::number(currentValue));
    painter.drawText(QRect(-60,-80,120,120),Qt::AlignCenter,QString::number(currentValue*4));
    QFont font1 = QFont("Arial",15);
    font.setBold(true);
    painter.setFont(font1);
    painter.drawText(QRect(-60,-30,120,120),Qt::AlignCenter,"Km/h");
}

//2.画刻度线
void Widget::drawScale(QPainter &painter,int radius)
{
    //1.算出一个刻度需要旋转的角度
    //2.设置第一个刻度的位置
    painter.save();//保存操作前的角度
    painter.setPen(QPen(Qt::white,5));
    painter.rotate(startAngle);//将坐标系旋转,画第一个刻度
    for(int i = 0;i<=60;i++){//六十个刻度
        if(i>=40){
            painter.setPen(QPen(Qt::red,5));
        }
        if(i % 5 == 0){
//            if(150 + angle *i <= 270){ //150是0刻度
//                painter.rotate(180);
//                painter.drawText(-(radius-30),8,QString::number(i*4));//画数字
//                painter.rotate(-180);
//            }else{
//                painter.drawText((radius-70),8,QString::number(i*4));//画数字
//            }
            painter.drawLine(radius-20,0,radius-3,0);//画长的刻度线
        }else{
            painter.drawLine(radius-8,0,radius-3,0);//画短的刻度线
        }
        painter.rotate(angle);//画完之后将角度旋转，画下一个刻度
    }
    painter.restore();
    painter.setPen(QPen(Qt::white,5));

}

//3.画刻度线数字
void Widget::drawScaleText(QPainter &painter, int radius)
{
    //写刻度文字
    QFont font("Arial",15);
    font.setBold(true);
    painter.setFont(font);
    int r = radius-49;//离远点
    for(int i = 0;i <= 60;i++){

        if(i % 5 == 0 ){
        //保存坐标系
        painter.save();
        //算出坐标系
        int delX = qCos(qDegreesToRadians(210-angle*i)) * r;//Qt中sin认得是弧度
        int delY = qSin(qDegreesToRadians(210-angle*i)) * r;//弧度=角度*3.14/180
        //平移坐标系
        painter.translate(QPoint(delX,-delY));
        //旋转坐标系
        painter.rotate(-120+angle*i);
        //写上文字
        painter.drawText(-25,-25,50,30,Qt::AlignCenter,QString::number(i*4));
        //恢复坐标系
        painter.restore();
        }
    }
}


//4.画变速器指针
void Widget::drawPointLine(QPainter &painter, int lenth)
{
    //painter.restore();//恢复坐标轴
    painter.save();
    painter.setBrush(Qt::white);
    painter.setPen(Qt::NoPen);

    static const QPointF points[4] = {
        QPointF(0,0),
        QPointF(200.0,-1.1),
        QPointF(200.0,1.1),
        QPointF(0,15.0),
    };


    painter.rotate(startAngle + angle * currentValue);
    painter.drawPolygon(points,4);

    //painter.drawLine(60,0,lenth,0);
    painter.restore();
}

//5.画速度扇形
void Widget::drawSpeedPie(QPainter &painter, int radius)
{
    painter.save();

    QRect rentangle(-radius,-radius,radius*2,radius*2);
    painter.setPen(Qt::NoPen);//去边框
    painter.setBrush(QColor(255,150,0,100));
    painter.drawPie(rentangle,(360-startAngle)*16,-angle*currentValue*16);
    painter.restore();
}

//开启启动
void Widget::startSpeed()
{
    timer = new QTimer(this);

    connect(timer,&QTimer::timeout,[=](){
        currentValue += flag;
        if(currentValue == 60)
            flag = -1;
        if(currentValue == 0)
            flag = 1;
        update();
    });
    timer->start(50);
}


//7.画中间的黑色圆
void Widget::drawEllopseInnerBlack(QPainter &painter,int radius)
{
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(0,0),80,80);
}

//6.画内圆的渐变色圈
void Widget::drawEllopseInnerShine(QPainter &painter, int radius)
{
    painter.setPen(Qt::NoPen);
    QRadialGradient radiaGradient(0,0,radius);
    radiaGradient.setColorAt(0.0,QColor(255,0,0,200));
    radiaGradient.setColorAt(1.0,QColor(255,0,0,100));
    painter.setBrush(radiaGradient);
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

//9.画外部的渐变色圈
void Widget::drawEllopseOutterShine(QPainter &painter, int radius)
{
    painter.save();
    QRect rentangle(-radius,-radius,radius*2,radius*2);
    painter.setPen(Qt::NoPen);//去边框

    QRadialGradient radiaGradient(0,0,radius);
    radiaGradient.setColorAt(1,QColor(255,0,0,200));
    radiaGradient.setColorAt(0.97,QColor(255,0,0,120));
    radiaGradient.setColorAt(0.9,QColor(0,0,0,0));
    radiaGradient.setColorAt(0,QColor(0,0,0,0));

    painter.setBrush(radiaGradient);

    //painter.setBrush(QColor(255,0,0,100));
    painter.drawPie(rentangle,(360-150)*16,-angle*60.5*16);
    painter.restore();

}

//画logo
void Widget::drawLogo(QPainter &painter, int radius)
{
    QRect rectangle(-50,radius*0.4,100,100);
    painter.drawPixmap(rectangle,QPixmap(":/icon/mi.png"));
}


void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //初始化画布
    initCanvas(painter);

    //画小圆
   //drawMiddleCirle(painter,60);


//    //设置渐变色属性
//    QRadialGradient radialGradient(0,0,radius);
//    //设置渐变色颜色
//    radialGradient.setColorAt(0.0,QColor(255,0,0,50));
//    radialGradient.setColorAt(1.0,QColor(255,0,0,250));

//    //使用这个渐变创建画刷QBrush
//    QBrush brush(radialGradient);
//    painter.setBrush(brush);


    //画刻度
    drawScale(painter,radius);

    //画刻度文字
    drawScaleText(painter,radius);

    //画指针线
    drawPointLine(painter,radius-58);

    //画扇形
    drawSpeedPie(painter,radius+25);

    //画黑色内圈外围的渐变圈
    drawEllopseInnerShine(painter,110);

    //画黑色内圈
    drawEllopseInnerBlack(painter,80);

    //画当前速度值
    drawCurrenSpeed(painter);

    //画外环发光圈
    drawEllopseOutterShine(painter,radius+25);

    //画一个logo
    drawLogo(painter,radius);
}
